<?php 
  include_once('session.php');
  include_once('db.php');
  include_once('constants.php');
  
  //header("Content-Type: application/xml; charset=utf-8");
  header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  header('Content-Disposition: attachment; filename="onmotion.kml"');
  
  //test query http://localhost/onmotion/listFriends.php?userLogin=SergioEstevao&userTid=123456
            
  $doc = new DomDocument('1.0', 'UTF-8');
  $doc->formatOutput = true;
  $kmlNs = $doc->createElementNS('http://earth.google.com/kml/2.1','kml');
  $doc->appendChild($kmlNs);
   
  $document = $doc->createElement('Document');
  
  $name = $doc->createElement('name');
  $nameValue = $doc->createTextNode($product_name.' Document ');
  $name->appendChild($nameValue);
    
  $description = $doc->createElement('description');
  $descriptionValue = $doc->createTextNode($product_name.' - Invalid Security');
  $description->appendChild($descriptionValue);
  
  $document->appendChild($name);
  $document->appendChild($description);
  
  $schema = $doc->createElement('Schema');
  $schema->setAttribute("parent", "Placemark");
  $schema->setAttribute("name", "FindMePlacemark");
  $fieldDate = $doc->createElement('SimpleField');
  $fieldDate->setAttribute("type","wstring");
  $fieldDate->setAttribute("name","date");
  
  $schema->appendChild($fieldDate);
  
  $fieldImage = $doc->createElement('SimpleField');
  $fieldImage->setAttribute("type","wstring");
  $fieldImage->setAttribute("name","image");
  $schema->appendChild($fieldImage);
  
  $document->appendChild($schema);
  
  $fieldOwner = $doc->createElement('SimpleField');
  $fieldOwner->setAttribute("type","wstring");
  $fieldOwner->setAttribute("name","user");
  $schema->appendChild($fieldOwner);
  
  $document->appendChild($schema);
                  
  $description->replaceChild($doc->createTextNode('Other People'), $descriptionValue);
  $conn = &ADONewConnection($dbType);
  $conn->PConnect($dbServer, $dbUser, $dbPassword, $dbName);
  //$conn->debug = true;   
  $query  = "SELECT lat, lng, altitude, point.name, comment, max(time), user.id_user, user.name FROM point, user ";
  $query .= "WHERE point.id_user=user.id_user AND user.publicpos=1 ";
  $query .= "GROUP BY user.name ORDER BY user.name";
  //execute query         
  $rs = $conn->Execute($query);
            
  // create Point Placemark
  foreach ($rs as $row) 
  {
    $placeMark = $doc->createElement('FindMePlacemark');
    $placeMark->setAttribute("id",$row[6]);
    $name = $doc->createElement('name');
    $nameValue = $doc->createTextNode($row[3]);
    $name->appendChild($nameValue);
    $placeMark->appendChild($name);
    
    $description = $doc->createElement('description');
    $descriptionValue = $doc->createTextNode($row[4]);
    $description->appendChild($descriptionValue);
    $placeMark->appendChild($description);
    
    $date = $doc->createElement('date');
    $dateValue = $doc->createTextNode($row[5]);
    $date->appendChild($dateValue);
    $placeMark->appendChild($date);
    
    $user = $doc->createElement('user');
    $userValue = $doc->createTextNode($row[7]);
    $user->appendChild($userValue);
    $placeMark->appendChild($user);
    
    $point = $doc->createElement('Point');
    $coordinates = $doc->createElement('coordinates');
    $pointValue = $doc->createTextNode($row[1].','.$row[0].','.$row[2]);
    $coordinates->appendChild($pointValue);
    $point->appendChild($coordinates);
    $placeMark->appendChild($point);                         
    
    // select resources
    $image = $doc->createElement('image');
    $imageText = '0';            
    $queryResource = "select id_image from image where id_point=$row[6]";
    $rsResource = $conn->Execute($queryResource);
    foreach($rsResource as $rowResource) {          
        $imageText = $rowResource[0];
    }
    $imageValue = $doc->createTextNode($imageText);
    $image->appendChild($imageValue);
    $placeMark->appendChild($image);      
    
    $rsResource->close();
                
    $document->appendChild($placeMark);     
  }
  
  $rs->close();
  $conn->close();
               
  $kmlNs->appendChild($document);        
     
  // save to xml file
  $xml_string = $doc->saveXML();
  // output in UTF-8
  //echo iconv("ISO-8859-1", "UTF-8",$xml_string);
  echo $xml_string;
?>
